package com.cq.xjms.service.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.extremecomponents.table.limit.Limit;

import com.cq.xjms.model.Course;
import com.cq.xjms.service.CourseManager;

public class CourseManagerImpl extends BaseManagerImpl<Course> implements CourseManager{
	private static Logger LOG = Logger.getLogger(CourseManagerImpl.class);
	
	public List<Course> queryCourses(String name,String code,Long teacherId,Limit limit) {
		StringBuffer hql = new StringBuffer();
		hql.append("select c from Course c left join fetch c.teacher t where 1=1 ");
		if(StringUtils.isNotEmpty(name)){
			hql.append(" and c.name=:name");
		}
		if(StringUtils.isNotEmpty(code)){
			hql.append(" and c.code=:code");
		}
		if(teacherId!=null){
			hql.append(" and c.teacher.id=:teacherId");
		}
		hql.append(" order by c.createDate desc");
		List<Course> courses = null;
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(name)){
				query.setParameter("name", name);
			}
			if(StringUtils.isNotEmpty(code)){
				query.setParameter("code", code);
			}
			if(teacherId!=null){
				query.setParameter("teacherId", teacherId);
			}
			if (limit != null) {
				query.setFirstResult((limit.getPage() - 1)
						* limit.getCurrentRowsDisplayed());
				query.setMaxResults(limit.getCurrentRowsDisplayed());
			}
			courses =  query.getResultList();
		}catch(Exception e){
			LOG.error("query course failed!", e);
		}
		return courses;
	}
	
	

	@Override
	public int countCourse(String name,String code,Long teacherId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(c) from Course c  where 1=1 ");
		if(StringUtils.isNotEmpty(name)){
			hql.append(" and c.name=:name");
		}
		if(StringUtils.isNotEmpty(code)){
			hql.append(" and c.code=:code");
		}
		if(teacherId!=null){
			hql.append(" and c.teacher.id=:teacherId");
		}
		Object result = null;
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(name)){
				query.setParameter("name", name);
			}
			if(StringUtils.isNotEmpty(code)){
				query.setParameter("code", code);
			}
			if(teacherId!=null){
				query.setParameter("teacherId", teacherId);
			}
			result = query.getSingleResult();
		}catch(Exception e){
			LOG.error("query course failed!", e);
		}
		return result == null ? 0: result instanceof Integer ? ((Integer) result).intValue():((Long) result).intValue();
	}



	@Override
	public int countCourseBy(int status, String yearterm) {
		// TODO Auto-generated method stub
		
		StringBuffer hql = new StringBuffer();
		hql.append("select count(c) from Course c  where 1=1 ");
		if(StringUtils.isNotEmpty(yearterm)){
			hql.append(" and c.term like:yearterm");
		}
		if(status !=0){
			hql.append(" and c.status=:status");
		}
		
		
		Object result = null;
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(yearterm)){
				query.setParameter("yearterm", "%"+yearterm+"%");
			}
			
			if(status !=0){
				query.setParameter("status", status);
			}
			result = query.getSingleResult();
		}catch(Exception e){
			LOG.error("query course failed!", e);
		}
		return result == null ? 0: result instanceof Integer ? ((Integer) result).intValue():((Long) result).intValue();
	}



	@Override
	public List<Course> queryCoursesBy(int status, String yearterm, Limit limit) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		hql.append("select c from Course c left join fetch c.teacher t where 1=1 ");
		if(StringUtils.isNotEmpty(yearterm)){
			hql.append(" and c.term like:yearterm");
		}
		if(status !=0){
			hql.append(" and c.status=:status");
		}
		
		hql.append(" order by c.createDate desc");
		List<Course> courses = null;
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(yearterm)){
				query.setParameter("yearterm","%"+yearterm+"%");
			}
			
			if(status !=0){
				query.setParameter("status", status);
			}
			if (limit != null) {
				query.setFirstResult((limit.getPage() - 1)
						* limit.getCurrentRowsDisplayed());
				query.setMaxResults(limit.getCurrentRowsDisplayed());
			}
			courses =  query.getResultList();
		}catch(Exception e){
			LOG.error("query course failed!", e);
		}
		return courses;
		
	}
	
}
